<?php

namespace app\hospital\controller;
use app\hospital\model\doctoruser;
use app\hospital\model\doctor;
use app\hospital\model\patientreserve;
use app\hospital\model\subjecttype;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;

class index
{
    function  index()
    {
        $data=doctor::order('id','desc')->paginate(["list_rows"=>10,"query"=>Request::param()]);
        View::assign(['data'=>$data,'user'=>Session::get('name'),'role'=>Session::get('role')]);
        return View::fetch();
    }
    function login()
    {
        $m=Request::method();
        if($m=="GET"){
            return View::fetch();
        }else{
            $u=Request::param('user');
            $p=Request::param('pwd');
            $id=Request::param('id');
            $user=doctoruser::where(['name'=>$u,'password'=>$p,'id'=>$id])->find();
            if($user){
                Session::set('name',$user['name']);
                Session::set('id',$user['id']);
                Session::set('role',$user['role']);
                Session::set('pwd',$user['password']);
                return '登录成功';
            }else{
                return '用户名或密码有误，登录不成功';
            }
        }
    }
    function logout()
    {
        Session::delete('name');
        Session::delete('id');
        Session::delete('role');
        Session::delete('pwd');
        return redirect('/index.php/hospital/index');
    }
    function register(){
        $a=Db::table("doctoruser")->order("id","desc")->find();
        $id=$a['id']+1;
//        dump($a);
//        dump($id);
        View::assign("id",$id);
       return View::fetch();
    }
    function adduser()
    {
        $p=Request::post();
        $res=Db::table('doctoruser')->insert($p);
//        $res=Db::table('subjecttype')->insert($p);
        if(!empty($res)){
            return "注册成功";
        }else{
            return "注册失败";
        }
    }
    public function reserve(){
        $stime=date('Y-m-d');
        $d=new doctor();
        $d1=new subjecttype();
        $m3=$d->paginate();
        $m4=$d1->paginate();
        View::assign(['rid'=>Session::get('id'),'rname'=>Session::get('name'),'m3'=>$m3,'m4'=>$m4,'time'=>$stime]);
//        dump(Session::get('id'));
        return View::fetch();
    }
    public function receive(){
        $stime=date('Y-m-d');
        $key=Request::param('key');
        if(!empty($key)){
            $d=new patientreserve();
            $m3=$d->where(['doctorname'=>Session::get('name'),'state'=>'未看诊'])
                ->where('patientname','like','%'.$key.'%')
                ->paginate(5);
        }else{
            $m3=Db::name("patientreserve")->where(['state'=>'未看诊','rtime'=>$stime,'doctorname'=>Session::get('name')])->paginate(5);
        }
        View::assign(['data'=>$m3,'stime'=>$stime]);
//        dump(Session::get('id'));
        return View::fetch();
    }
    function edit(){
        $id=Request::get('id');
//        dump($id);
        $data=patientreserve::Where('num','=',$id)->find();
//        $data=Db::table('doctor')->find($id);
        return json($data);
    }
    public function order(){
        $key=Request::param('key');
        if(!empty($key)){
            $d=new patientreserve();
            $m3=$d->where(['patientid'=>Session::get('id')])
                ->whereor('doctorname','like','%'.$key.'%')
                ->whereOr('rtime','like','%'.$key.'%')
                ->paginate(5);
        }else{
            $m3=Db::name("patientreserve")->where('patientid',"=",Session::get('id'))->paginate(5);
        }
        View::assign(['m3'=>$m3]);
//        dump(Session::get('id'));
        return View::fetch();
    }
    function historylist(){
        $key=Request::param('key');
        if(!empty($key)){
            $d=new patientreserve();
            $data=$d->search();
        }else{
            $data=Db::table('patientreserve')->where(['doctorname'=>Session::get('name'),'state'=>'已看诊'])->paginate(5);
        }
        View::assign('data',$data);
        return View::fetch();
    }
    public function add(){
        $p=Request::post();
        $d=new patientreserve();
        $res=$d->insert($p);
//        $res=Db::table('subjecttype')->insert($p);
        if(!empty($res)){
            return 1;
        }else{
            return 0;
        }
//        dump($p);
//        dump($res);
//        return $res;
    }
    public function update(){

        $p=Request::post();
        $d=new patientreserve();
        $res=$d->where('num','=',$p['num'])->update($p);
//        $res=$d->insert($p);
//       $res=Db::table('subjecttype')->insert($p);
        if(!empty($res)){
            return 1;
        }else{
            return 0;
        }
//        dump($p);
//        dump($res);
//        return $res;
    }
    function list(){
        $key=Request::param('key');
        $d=new doctor();
        if(!empty($key)){
            $data=$d->search();
        }else{
            $data=$d->paginate(5);
        }
        View::assign('data',$data);
        return View::fetch();
    }
    function list1(){
        $key=Request::param('key');
        $d=new subjecttype();
        if(!empty($key)){
            $data=$d->search();
        }else{
            $data=$d->paginate(5);
        }
        View::assign('data',$data);
        return View::fetch();
    }
    function r(){
        $d='请先登录';
        View::assign('d',$d);
        return View::fetch();
    }
    function mycenter(){
        View::assign(['id'=>Session::get('id'),'name'=>Session::get('name')]);
        return View::fetch();
    }
    function updatepwd(){
        return View::fetch();
    }
    function updatep(){
        $p=Request::param();
        if($p['password1']!=Session::get('pwd')){return "原密码有误";}
        if($p['password']!=$p['password2']){return "两次密码输入不一致";}
        if($p['password1']==Session::get('pwd')&&$p['password']==$p['password2']){
            $d=new doctoruser();
            $res=$d->where('id','=',Session::get('id'))->update(['password'=>$p['password']]);
            if(!empty($res)){
                return "修改成功!!请退出重新登录！";
            }else{
                return "修改失败";
            }
        }
    }
    function queue(){
        $stime=date('Y-m-d');
            $key=Request::param('key');
            if(!empty($key)){
                $data=Db::table('patientreserve')->where(['state'=>'未看诊','rtime'=>$stime,'doctorname'=>$key])->select();
            }else{
                $data=Db::table('patientreserve')->where(['state'=>'未看诊','rtime'=>$stime])->paginate(5);
            }
            View::assign('data',$data);
            return View::fetch();
    }
}